<template>
	<view v-if="showPage">
		<view class="address" v-for="item in address" :key="item.id">
			<view class="address_top" @click="toAddress">
				<view class="address_top_left">
					<image src="../../../static/images/Positioning_icon_03.png"></image>
				</view>
				<view class="address_top_right">
					<view class="Receiver">
						<view>收货人：{{item.name}}</view>
						<view>{{item.mobile}}</view>
					</view>
					<view class="Shipping_address">
						<view>收货地址：{{item.address}}</view>
					</view>
				</view>
				<view style="margin-top:20upx;margin-right: 20upx;">
					<image src="../../../static/images/arrow_icon_03.png" style="width:43upx;height: 52upx;" mode="aspectFit"></image>
				</view>
			</view>
			<view class="bg"></view>
		</view>
		<view class="indentList">
			<view class="indentList_items" v-for="item in goods_data" :key="item.productId">
				<view class="indentList_item">
					<view class="item_img">
						<image :src="item.goodImg" mode="widthFix"></image>
					</view>
					<view class="item_content">
						<view>{{item.goodName}}</view>
						<!-- <view>{{itemList.item_content_count}}片</view> -->
					</view>
					<view class="item_price">
						<text class="sellingPrice">￥{{item.sellingPrice}}</text>
                        <text class="sum">x{{item.sum}}</text>		
					</view>
				</view>
			</view>
		</view>
		<view class="orderPrice">
			<!-- <view class="orderPrice_item" v-if="showAdd">
				<view>商品数量</view>
				<view>
					<view class="subtract" @click="subtract">-</view>
					<view class="count" @click="count">{{goodsSum}}</view>
					<view class="add" @click="add">+</view>
				</view>
			</view> -->
			<view class="orderPrice_item">
				<view>商品总价</view>
				<view>￥{{sellingPrice}}</view>
			</view>
			<view class="orderPrice_item">
				<view>运费</view>
				<view>{{postage}}</view>
			</view>
			<view class="orderPrice_item">
				<view>订单总价</view>
				<view>￥{{orderPrice}}</view>
			</view>
		</view>
		<view style="width:100%;height: 20upx;background: #F5F5F5;"></view>
		<!-- 优惠券 -->
		<view class="coupons">
			<view class="coupons_left leftfloat">
				<text>优惠卷</text>
			</view>
			<view class="coupons_right rightfloat">
				<text>- ¥ {{discounts}} </text>
				<image src="../../../static/images/arrow_icon_03.png"></image>
			</view>
		</view>
		<!-- 结算导航 -->
		<view class="settlement">
			<view class="settle_left leftfloat">
				合计： <text>{{allPrice}}</text>
			</view>
			<view class="settle_middle leftfloat">已优惠¥{{discounts}}</view>
			<view class="settle_right leftfloat"  @click="buyGoods">去支付</view>
		</view>
		<view style="width:100%;height: 30%;background: #F5F5F5;"></view>
	</view>
    
</template>

<script>
	export default {
		data() {
			return {
				ordersListData: [],
				ordersListDataLast:[],
				productId:[],
				commodity:[],
				address:[],
				goods_data:[],
				orderNum:[],
				//运费
				postage:"0",
				//优惠
				discounts:0,
				//商品总价
				sellingPrice:"",
				//订单总价
				orderPrice:"",
				//合计
				allPrice:"",
				//收货地址id
				address_id:"",
				//订单id
				orderID:"",	
				//订单序列号
				orderNumber:"",
				//数量
				goodsSum:"",
				showPage:false,
				page:"",
				showAdd:false
			}
		},
		methods: {
			//显示加减数量
			showCount(){
				if(this.page=="shoppingCar"){
					this.showAdd=false
				}else{
					this.showAdd=true
				}
			},
			//减数量
			subtract(){
				let that=this;
				if(that.goodsSum>0){
					that.goodsSum--;
				}
			},
			//数量
			count(){
              
			},
			//加数量
			add(){
				let that=this;
				if(that.goodsSum>0){
					that.goodsSum++;
				}
			},
			//跳转地址
			toAddress(){
				uni.navigateTo({
					url:"../addressManagement/addressManagement"
				})
			},
			getData(){
				var that=this;
				let oldCommod=[]
				for(let i=0;i<that.productId.length;i++){
					let oldCommodity={"id":1,"sum":null}
					oldCommodity.id=that.productId[i];
					oldCommodity.sum=that.orderNum[i]
					let oldCommodityArry=[oldCommodity]
					if(that.page=="shoppingCar"){
						oldCommod.push(oldCommodity)			
					}else{
						if(that.productId.length>1){
							that.commodity.push(oldCommodity)
						}else{
							that.commodity=oldCommodityArry
						}	
					}			
				}
				if(that.page=="shoppingCar"){
					if(oldCommod.lenght>that.productId.length){
						that.commodity=oldCommod[0]
					}else{
						that.commodity=oldCommod
					}
				}
				
				//数据加载提示框
				uni.showLoading({
					title: '拼命加载中',
					mask: true
				});
				uni.request({
				    url:this.$store.state.URL + '/goods/good/settlement_price',
				    data:{
					   token:this.$store.state.token,
					   address_id:"",
					   commodity:that.commodity
				    },
					method:"GET",
					success(res) {
						let addressArry=[res.data.data.address]
						that.address=addressArry
						let goods_dataArry=res.data.data.goods_data
						that.goods_data=res.data.data.goods_data;
						console.log(res);
						//总计
						that.allPrice=res.data.data.payPrice;
						//商品总价
						that.sellingPrice=res.data.data.sellingPrice
						//订单总价
						that.orderPrice=res.data.data.orderPrice
						//运费
						that.postage=res.data.data.postage;
						//数量
						that.goodsSum=res.data.data.goods_data[0].sum
						console.log(that.postage);
						uni.hideLoading();
						//检查是否设置默认收货地址
						if(res.data.data.address==null){
							uni.showToast({
								title:"未设置默认收货地址",
								duration:1000,
								icon:"none",   
								mask:true
							})
							setTimeout(function(){
								uni.navigateTo({
									url:"../addressManagement/addressManagement"
								})
							},1000)
						}else{
							that.showPage=true
						}
					}
			    })
			},
			buyGoods(){
				//数据加载提示框
				uni.showLoading({
					title: '跳转支付中',
					mask: true
				});
				let that=this;
				that.address_id=that.address[0].id
				uni.request({
				    url:this.$store.state.URL + '/order/order/create_order',
				    data:{
					   token:this.$store.state.token,
					   address_id:that.address_id,
					   commodity:JSON.stringify(that.commodity)
				    },
					header:{
						'Content-Type': 'json',
					},
					method:"POST",
					success(res) {
						console.log(res)
						if(res.data.status==true){
							that.orderID=res.data.orderID
							let payment=res.data.data.data;
							let timeStamp=res.data.data.data.timeStamp.toString()
							uni.hideLoading();
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: timeStamp,
								nonceStr: payment.nonceStr,
								package: payment.package,
								signType: payment.signType,
								paySign: payment.paySign,
								success: function (res) {
									uni.showToast({
										title:"支付成功",
										duration:1000,
										icon:"none",
										mask:true
									})
									setTimeout(function(){
										uni.redirectTo({
											url:"../myOrders/myOrders?orderID="+that.orderID
										})
									},1000)
								},
								fail: function (err) {
									uni.showToast({
										title:"支付失败",
										duration:1000,
										icon:"none",
										mask:true
									})
									setTimeout(function(){
										uni.redirectTo({
											url:"../myOrders/myOrders?orderID="+that.orderID
										})
									},1000)
								}
							});
						}	
						else{
							uni.hideLoading();
							uni.showToast({
								title:"发起支付失败",
								duration:1000,
								icon:"none"
							})
						}
					},
					fail(res) {
						uni.hideLoading();
						uni.showToast({
							title:"失败",
							duration:1000,
							icon:"none"
						})
					},
					complete() {
						uni.hideLoading();
					}
				})
			}
		},
		onLoad(options) {
			//页面标识
			this.page=options.page
			//购物车
			this.ordersListData=JSON.parse(options.orderMessage);
			for(let i=0;i<this.ordersListData.orderInfo.length;i++){
		         this.ordersListDataLast[i]=JSON.parse(this.ordersListData.orderInfo[i])	
				 this.productId[i]=this.ordersListDataLast[i].productId
				 this.orderNum[i]=this.ordersListDataLast[i].orderNum
			}
			//显示加减数量
			this.showCount();
			console.log(this.$store.state.token,"用户token");
		},
		onShow() {
			//商品详情
			this.getData();
		},
		computed:{
			
		}
	}
</script>

<style>
 .obligation_header{
	  width:100%;
	  height:170upx;
	  background:#f2526f;
  }
  .obligation_header image{
	  width:123upx;
	  height:103upx;
	  margin-left:94upx;
	  margin-top:30upx;
	  margin-right: 30upx;
  }
  .obligation_header text{
	  font-size: 28upx;
	  color:#fff;
      position: relative;
	  top:-25upx;
  }
  .Receiver{
  	display: flex;
  	justify-content:flex-start;
  	height:40upx;
  	line-height: 40upx;
  }
  .Receiver view:nth-of-type(1){
  	font-size:32upx;
  	color:#222222;
  	margin-bottom: 10upx;
  }
  .Receiver view:nth-of-type(2){
  	font-size:24upx;
  	color:#222222;
  	margin-right:20upx;
	margin-left: 20upx;
  }
  .Shipping_address{
  	font-size:24upx;
  	color:#7b7b7b;
	margin-top:15upx;
  }
  .address{
  	background: #fff;
  	width:100%;
  	height:auto;
  	padding:20upx 0;
  	margin-bottom: 20upx;
  }
  .address_top{
  	display: flex;
	padding:20upx 0;
  }
  .address_top_left{
  	margin-left:30upx;
  }
  .address_top_right{
  	margin-left:20upx;
	width: 100%;
  }
  .address_top_left image{
  	width:43upx;
  	height:52upx;
  	margin:26upx 0;
  }
  .bg{
	  width:100%;
	  height:30upx;
	  background:#F5F5F5;
  }
  .list_id{
	  font-size:24upx;
	  color:#999999;
	  margin-left:30upx;
	  margin-top:30upx;
	  display: block;
  }
  .indentList_items{
	  margin-top:30upx;
	  border-bottom: 1upx solid #e0e0e0;
	  padding-bottom: 30upx;
  }
  .item_img{
	  margin-left:30upx;
  }
  .item_img image{
	  width:167upx;
	  heihgt:147upx;
  }
  .indentList_item{
	  display:flex;
	 justify-content: space-around;
	 /* padding-right: 30upx; */
  }
  .item_content{
	  margin-left:10upx;
  }
  .item_content view:nth-of-type(1){
	  font-size:30upx;
	  color:#222222;
	  line-height: 20px;
      height: 40px;
      white-space: pre-wrap;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      width:370upx;
      word-wrap:break-word; 
  }
  .item_content view:nth-of-type(2){
	  font-size:24upx;
	  color:#999;
	  margin-top:30upx;
  }
  .item_price{
	  text-align: right;
  }
  .sellingPrice{
	   font-size:26upx;
	  color: #333333;
	  display: block;
	  font-weight: 600;
  }
  .sum{
	 font-size:24upx;
	 color: #999;
	 display: block;
	 margin-top:20upx; 
  }
  .orderPrice{
	  padding:20upx 30upx;	  
  }
  .orderPrice_item{
	  display: flex;
	  justify-content: space-between;
  }
  .orderPrice_item view{
	  font-size:30upx;
	  color:#222222;
	  margin:5upx 0;
  }
.coupons{
	display: flex;
	justify-content: space-between;
	height: 80upx;
	line-height: 80upx;
	margin-top: 20upx;
	font-size: 28upx;
	background: #FFFFFF;
}
.coupons_left{
	margin-left: 20upx;
}
.coupons_right {
	margin-right: 20upx;
}
.coupons_right image{
	position:relative;
	top:5upx;
	margin-left: 10upx;
	width:15upx;
	height:30upx;
}
.settlement{
	position: fixed;
	bottom: 0upx;
	width: 750upx;
	height: 98upx;
	font-size: 30upx;
	background: #FFFFFF;
	text-align: center;
	display: flex;
	justify-content: space-between;
}
.settle_left{
	width: auto;
	height: 98upx;
	padding-top: 30upx;
	margin-left:30upx;
}
.settle_left text{
	color: #F2526F;
	font-weight: bold;
}
.settle_middle{
	width: 150upx;
	padding-top: 32upx;
	font-size: 24upx;
	text-align: left;
	color: #7B7B7B;
}
.settle_right{
	width: 252upx;
	background: #F2526F;
	height: 98upx;
	color: #FFFFFF;
	padding-top: 30upx;
}
.subtract{
	display: inline-block;
	height: auto;
	width:40upx;
	background:#fff;
	border:1upx solid #E8E8E8;
	text-align: center;
	border-radius: 8upx 0 0 8upx;
}
.count{
	display: inline-block;
	height: auto;
	width:60upx;
	background:#fff;
	border:1upx solid #E8E8E8;
	text-align: center;
}
.add{
	display: inline-block;
	height: auto;
	width:40upx;
	background:#fff;
	border:1upx solid #E8E8E8;
	text-align: center;
	border-radius:0 8upx 8upx 0;
}
</style>
